package com.hzx.ssm.controller;

import com.github.pagehelper.PageHelper;
import com.hzx.ssm.entity.Message;
import com.hzx.ssm.entity.ResBean;
import com.hzx.ssm.service.MessageService;
import com.hzx.ssm.service.impl.MessageServiceImpl;

import com.hzx.ssm.util.CodeByJedisUtil;
import com.hzx.ssm.util.CodeUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;


@Controller
@RequestMapping("/message")
public class MessageController {

    @Autowired
    private MessageServiceImpl messageService;

    //查找所有审核没有通过的墙消息，需要给管理员审核
    @ApiOperation("查找所有未审核的墙消息")
    @GetMapping(value = "findAllIsNot")
    @ResponseBody
    public ResBean findAllIsNot(@RequestParam("pageNum") Integer pageNum, @RequestParam("sizeNum") Integer sizeNum) {

        return ResBean.sucess(200, "查找成功", messageService.findAllIsNotMessage(pageNum, sizeNum));


    }

    //查找所有审核通过的墙消息
    @ApiOperation("查找所有已经通过审核的墙消息")
    @GetMapping(value = "findAllIs")
    @ResponseBody
    public ResBean findAllIS(@RequestParam("pageNum") String pageNum,
                             @RequestParam("sizeNum")  String sizeNum) {


        return ResBean.sucess(200, "查找成功", messageService.findAllIsMessage(Integer.parseInt(pageNum),
                Integer.parseInt(sizeNum)));

    }

    @ApiOperation("用户查找所有自己的墙消息")
    @ResponseBody
    @GetMapping("findMyAllMessage")
    public ResBean findMyAllMessage(@RequestParam("user_id") String user_id,
                                    @RequestParam("pageNum")int pageNum,
                                    @RequestParam("sizeNum")int sizeNum) {
        return messageService.findMyAllMessage(user_id,pageNum,sizeNum);
    }

    //根据id删除墙消息（可以删除所有）
    @ApiOperation("根据id删除墙消息（管理员使用）")
    @DeleteMapping(value = "deleteOneById")
    @ResponseBody
    public ResBean deleteOneById(@RequestParam("message_id") String message_id) {
        messageService.deleteMessageById(message_id);
        return ResBean.sucess(200, "删除成功");

    }

    @ApiOperation("根据id删除自己墙消息（用户使用）")
    @DeleteMapping(value = "deleteMyMessage")
    @ResponseBody
    public ResBean deleteMyMessage(@RequestParam("message_id") String message_id,
                                   @RequestParam("user_id") String user_id) {
       return messageService.deleteMyMessage(message_id, user_id);


    }

    //新增一个墙消息
    @ApiOperation("增加墙消息")
    @PostMapping(value = "addMessage")
    @ResponseBody
    public ResBean addMessage(Message message, HttpServletRequest request,
                              @RequestParam("verifyCodeActual") String verifyCodeActual) throws IOException {
        System.out.println(verifyCodeActual);
        System.out.println(message);
        if (!CodeByJedisUtil.chekVerifyCode(message.getMessage_userId(),verifyCodeActual))
            return ResBean.error(401, "验证码错误");

//        //下载图片
//        String originalFilename = file.getOriginalFilename();
//        //获取图片的后缀
//        String suffix = originalFilename.substring(originalFilename.lastIndexOf('.'));
//        String prefix = UUID.randomUUID().toString();
//        //创建用户图片保存的路径
//        String filePath = "C:" + File.separator + "Userimg";
//        File imgsLocation = new File(filePath);
//        //如果该路径不存在就创建
//        if (imgsLocation.mkdirs())
//            System.out.println("创建成功");
//
//        String fullPath = filePath + File.separator + prefix + suffix;
//        //传输图片
//        file.transferTo(new File(fullPath));
//
//        //设置墙信息的图片的文件路径
//        message.setMessage_imglocation(fullPath);
        messageService.addMessage(message);
        return ResBean.sucess(200, "添加成功");
    }

    //设置墙消息为显示
    @ApiOperation("管理员审核通过墙消息")
    @PutMapping(value = "setMessageEnable")
    @ResponseBody
    public ResBean setMessageEnable(@RequestParam("message_id") String message_id) {
        messageService.setMessageEnable(message_id);
        return ResBean.sucess(200, "审核通过");
    }

    @ApiOperation("用户点赞")
    @PutMapping("addUpNum")
    @ResponseBody
    public ResBean updateUpNum(@RequestParam("message_id") String message_id){
        return messageService.updateUpNum(message_id);
    }

}
